<template>
    <div id="app" :class="G_IsMobile ? 'mobile' : 'pc'">
        <!--      <loginPage></loginPage>-->
        <!--      <mainFrame v-else></mainFrame>-->
        <!-- <div class="text-3xl text-blue-500">{{b1}}-{{b2}}</div> -->
        <router-view :key="$route.fullPath"/>
    </div>
</template>

<script>
import loginPage from 'public/layout/login/loginPage.vue'
import { on, off } from 'view-design/src/utils/dom';
// import { setMatchMedia } from 'view-design/src/utils/assist';
//
import { mapState, mapMutations } from 'vuex';
import { typeClassName } from "@/pages/config/classNameMap";
import boardPath from "@/router/boardPath";
//
// setMatchMedia();

var mobileMaxWidth = 720;
var maxWidth = 1440;
var designWidth = 1280;
var rem2px = 100;
export default {
    name: 'app',
    components: { loginPage },
    data() {
		let a = {c: '中文'}
		let b1 = a?.c;
        let b2 = a.c1??'b2';

        return {
			b1, b2,
            collectionObject:this.$modelObject.create(`entityCollection`),
            timer:null,
            //站内通知页码
            noticePageNavigation: { total: 0, pageSize: 5, pageNumber: 1 },
            //站内通知默认筛选条件
            noticeFilter:{sf_status:'0',sf_account:''}
        }
    },
    computed: {
        ...mapState('admin/layout', ['noticeTabList','currentAccount','menu'])
    },
    watch:{
        '$route'(route){
            // if(route.name==='StarCloud' && this.menu[0].children && this.menu[0].children[0]){
            //     //设置首页重定向
            //     this.$router.replace({path:this.menu[0].children[0].path})
            if(route.name==='StarCloud'&&route.query.redirect){
                //设置首页重定向
                this.$router.replace(route.query.redirect);
            }else if(route.name==='login'||boardPath.includes(route.path)){
                //退出登录后清除定时器
                this.timer && clearInterval(this.timer)
            }
        }
    },
    methods: {
        ...mapMutations('admin/layout', [
            'setDevice',
            'setNotice',
        ]),
        handleWindowResize() {
            let isMobile = false
            let win = window

            win.document.documentElement.style.fontSize = (
                (Math.min(win.innerWidth, maxWidth) / designWidth) * rem2px
            ) + 'px';
            if (win.innerWidth <= mobileMaxWidth) {
                isMobile = true;
            }
            win.isMobile = isMobile
            this.setDevice(isMobile);
        },
        // handleMatchMedia () {
        //     const matchMedia = window.matchMedia;
        //
        //     if (matchMedia('(max-width: 600px)').matches) {
        //         this.setDevice('Mobile');
        //     } else if (matchMedia('(max-width: 992px)').matches) {
        //         this.setDevice('Tablet');
        //     } else {
        //         this.setDevice('Desktop');
        //     }
        // }
        /**
         * 获取站内信
         */
        getNoticeData(pageNavigation,filter){
            filter = {
                className: typeClassName['notice'].className,
                type: typeClassName['notice'].type,
                ...filter,
            }
            let newFilter = '';
            if (this.tools.isReal(filter)) {
                newFilter = this.tools.getFilter(filter);
            }
            // this.$api.EntityInstance.queryByPage(pageNavigation.pageNumber, pageNavigation.pageSize, {condition:newFilter}).then(res=>{
            //     pageNavigation.total = res.recordCount;
            //     this.setNoticeList(pageNavigation,filter,res.records)
            // })
        },
        setNoticeList(pageNavigation,filter,records){
            let currentTab = {};
            let tabIndex = this.noticeTabList.findIndex(d=>d.status===filter.sf_status)
            if(tabIndex===-1) return;
            currentTab = this.noticeTabList[tabIndex];
            currentTab.messageLoading = false;
            currentTab.count = pageNavigation.total;
            currentTab.hasLoadedAll = currentTab.count <= pageNavigation.pageSize*pageNavigation.pageNumber
            let resData = records.map(d=>{
                return {title:d.values.sf_title,time:Number(d.values.sf_timeStamp),id:d.id,path:d.values.sf_path}
            })
            if(pageNavigation.pageNumber===1){
                currentTab.messageList = resData;
            }else{
                currentTab.messageList = [...currentTab.messageList, ...resData]
            }
            let noticeTabList = this.noticeTabList
            noticeTabList.splice(tabIndex,1,currentTab)
            store.commit('admin/layout/setNotice',noticeTabList);
        },
        interval(){
            if(this.timer){
                clearInterval(this.timer)
            }else{
                this.timer = setInterval(()=>{
                    let page = { total: 0, pageSize: this.noticePageNavigation.pageSize*this.noticePageNavigation.pageNumber, pageNumber: 1 }
                    this.noticeFilter.sf_account = this.currentAccount ? this.currentAccount.account : ''
                    this.getNoticeData(page,this.noticeFilter)
                },60000*2)
            }
        },
        noticeChangeStatus(data){
            let {tab,item,status,oldStatus} = data;
            const className = typeClassName['notice'].className;
            const type = typeClassName['notice'].type;
            let filter = {className, type, sf_status:oldStatus, sf_account:this.currentAccount.account}
            let newFilter = '';
            if (this.tools.isReal(filter)) {
                newFilter = this.tools.getFilter(filter);
            }
            let params = {className, type, values: {sf_status: status }};
            !!item && (params.id = item.id)
            this.$api.EntityInstance.updateByCondition(newFilter,params).then(res=>{
                //后台返回更新的条目数
                if(params.id === undefined){
                    this.changeAllStatus({tab,count:res})
                }else{
                    this.changeOneStatus({id:params.id,tab,res})
                }
            })
        },
        //修改单个状态
        changeOneStatus({id,tab,res}){
            let messageIndex = tab.messageList.findIndex(d=>d.id===id)
            if(messageIndex===-1) return;
            tab.count -= 1;
            tab.messageList.splice(messageIndex,1)
        },
        //修改tab中所有状态
        changeAllStatus({tab,count}){
            tab.count -= count;
            (tab.count < 0) && (tab.count = 0)
            tab.messageList = [];
            let noticeTabList = this.noticeTabList
            let tabIndex = this.noticeTabList.findIndex(d=>d.name===tab.name)
            if(tabIndex===-1) return;
            noticeTabList.splice(tabIndex,1,tab)
            store.commit('admin/layout/setNotice',noticeTabList);
        },
        goToPage(path){
            this.$router.push(path).catch(err => { return err })
        }
    },
    mounted() {
        window.addEventListener('resize', this.handleWindowResize)
        this.handleWindowResize()
        // on(window, 'resize', this.handleWindowResize);
        // this.handleMatchMedia();
        //站内信通知
        this.$EventBus.$on('getStationNotice',(data)=>{
            this.noticePageNavigation = data.pageNavigation
            this.noticeFilter = data.filter
            // this.getNoticeData(data.pageNavigation,data.filter)
        })
        this.$EventBus.$on('noticeChangeStatus',(data)=>{
            // this.noticeChangeStatus(data)
        })
        this.$EventBus.$on('goToPage',(className)=>{
            this.goToPage(className)
        })
        this.interval()
    },
    beforeDestroy() {
        window.removeEventListener('resize', this.handleWindowResize)
        // off(window, 'resize', this.handleWindowResize);
        this.$EventBus.$off('getStationNotice');
        this.$EventBus.$off('noticeChangeStatus');
        this.$EventBus.$off('goToPage');
        this.timer && clearInterval(this.timer)
    }
}
</script>
<style scoped></style>